﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;              //命名空间
using System.Data.SqlClient;    //数据库头文件
using System.Data.Sql;         //数据库头文件
using DATA_operate_;          //自定义数据库操作类引用
using System.Text;

public partial class design_page_admin_Default : System.Web.UI.Page
{
    DATA_operate data_operate_in_class_add = new DATA_operate();   //创建数据库操作类对象
       
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)   //非返回
        {
            //DropDownList1数据绑定
            SqlDataAdapter dap = new SqlDataAdapter("select _Tid from _teacher", data_operate_in_class_add.databaseopen());   //打开数据库绑定
            DataTable dt = new DataTable();
            dap.Fill(dt);   //数据导入
            DropDownList1.Items.Clear();
            DropDownList1.DataSource = dt;
            DropDownList1.DataTextField = "_Tid";//标志字段
            DropDownList1.DataValueField = "_Tid";
            DropDownList1.DataBind();//数据绑定   
            
            
            TextBox1.Text = Make_classid();
           

            SqlDataAdapter dap1 = new SqlDataAdapter("select _Cid from _class", data_operate_in_class_add.databaseopen());   //打开数据库绑定
            DataTable dt1 = new DataTable();
            dap1.Fill(dt1);   //数据导入
            DropDownList3.Items.Clear();
            DropDownList3.DataSource = dt1;
            DropDownList3.DataTextField = "_Cid";//标志字段
            DropDownList3.DataValueField = "_Cid";
            DropDownList3.DataBind();//数据绑定   
           


        }
        if (DropDownList2.SelectedItem.Text == "添加新班级")
        {
            TextBox1.Visible = true;
            TextBox1.Enabled = false;
            DropDownList3.Visible = false;
            TextBox4.Enabled = true;
            
            
        }
        else
        {
            TextBox1.Visible = false;
            DropDownList3.Visible = true;



         

            TextBox4.Enabled = false;
            TextBox4.Text=data_operate_in_class_add.dataread("_class","_Cname","_Cid",DropDownList3.SelectedItem.Text);
        }

        TextBox2.Text = data_operate_in_class_add.dataread("_teacher", "_Tname", "_Tid", DropDownList1.SelectedItem.Text);  //在TEXEBOX上显示选取的字段 

      
    }



    private string Make_classid()   //生成作业ID+++
    {

        string numA = data_operate_in_class_add.dataread("MAX(_Cid)", "_class", true);   //班级最大ID 号
        if(numA==null)
        {
            numA = "0000";
        }
        string numB = "1";   //最大ID 号加1
        
        int lenA = 0, lenB = 0;
        int lenI = 0, intTen = 0;
        int intA = 0, intB = 0;

        StringBuilder result = new StringBuilder("");
        lenA = numA.Length;
        lenB = numB.Length;
        if (lenA > lenB)
            lenI = lenA;
        else
            lenI = lenB;
        for (int i = 0; i < lenI; i++)
        {
            if (i < lenA)
                intA = Convert.ToInt16(numA.Substring(lenA - i - 1, 1));
            else
                intA = 0;
            if (i < lenB)
                intB = Convert.ToInt16(numB.Substring(lenB - i - 1, 1));
            else
                intB = 0;
            int temp = intA + intB + intTen;
            result.Insert(0, Convert.ToString((int)temp % 10));
            intTen = (int)temp / 10;
        }
        if (intTen > 0)
            result.Insert(0, Convert.ToString(intTen));
        return result.ToString();
    }




    protected void Button2_Click(object sender, EventArgs e)   //添加教师成员
    {
        //获得添加文本信息

        if (DropDownList2.SelectedItem.Text == "添加新班级")
        {
            if (TextBox1.Text == "")
            {
                Label7.Visible = true;
                Label7.Text = "  添加失败了 : 班级ID不能为空";
            }
            else if (TextBox4.Text == "")
            {
                Label7.Visible = true;
                Label7.Text = "  添加失败了 : 班级名称不能为空";
            }
            else
            {
                string classid = TextBox1.Text;   //班级ID
                string teacherid = DropDownList1.SelectedItem.Text;//教师ID
                string teachername = TextBox2.Text;//教师名称
                string classname = TextBox4.Text;//班级名称




                string addinfo = classid + "," + classname;
                if(!data_operate_in_class_add.dataadd("_class", addinfo))  //添加到_class里
                {
                    Label7.Text = "添加到班级表失败";
                    return;
                }
                string addinfo1 = classid + "," + teacherid;
                if(data_operate_in_class_add.dataadd("_teacher_class", addinfo1))   //添加到_teacher_class
                {
                    Label7.Visible = true;
                    Label7.Text = "添加成功了 : 班级ID:" + classid + "  班级名称:" + classname + " 教师名称:" + teachername;
                    Button3_Click(sender, e);
                }
                else
                    Label7.Text = "添加失败";
            }


        }
        else
        {
            string teachername = TextBox2.Text;//教师名称
            string classname = TextBox4.Text;//班级名称
            string classid = DropDownList3.SelectedItem.Text;   //班级ID
            string teacherid = DropDownList1.SelectedItem.Text;//教师ID
            string addinfo = classid + "," + teacherid;
            if(data_operate_in_class_add.dataadd("_teacher_class", addinfo))   //添加这个班级对应的老师
            {
                Label7.Visible = true;
                Label7.Text = "" + classname + "添加了新任课教师:" + teachername;
                Button3_Click(sender, e);
            }
            else
                Label7.Text = "添加失败";
           
        }
           
            
      

        

    }


    protected void select_tid_show_tname(object sender, EventArgs e)  //获得教师名称   并显示
    {
        //TextBox2.Text = data_operate_in_class_add.dataread("_teacher", "_Tname", "_Tid", DropDownList1.SelectedItem.Text);        
    }
    protected void Button3_Click(object sender, EventArgs e)   //置空
    {
        TextBox1.Text = Make_classid();
        TextBox4.Text = "";
    }
    protected void Button4_Click(object sender, EventArgs e)   //显示所有班级信息
    {

    }
}